package com.zzw.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.springboot.bean.Supplier;
import com.zzw.springboot.bean.SupplierEvaluation;
import com.zzw.springboot.service.SupplierEvaluationService;
import com.zzw.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supplier/evaluation")
public class SupplierEvaluationController {
    @Autowired
    private SupplierEvaluationService evaluationService;

    /**
     * 获取评价记录列表
     */
    @GetMapping("/list")
    public Result listEvaluations(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            SupplierEvaluation evaluation) {
        //创建lambdaQueryWrapper, 封装查询条件
        LambdaQueryWrapper<SupplierEvaluation> lambdaQueryWrapper = Wrappers.<SupplierEvaluation>lambdaQuery();
        Page<SupplierEvaluation> page = evaluationService.page(new Page<SupplierEvaluation>(pageNum, pageSize), lambdaQueryWrapper);
        return Result.success(page);
    }

    /**
     * 根据供应商ID获取评价记录
     */
    @GetMapping("/supplier/{supplierId}")
    public Result getEvaluationsBySupplierId(@PathVariable("supplierId") Integer supplierId) {
        List<SupplierEvaluation> list = evaluationService.getEvaluationsBySupplier(supplierId);
        return Result.success(list);
    }

    /**
     * 获取评价记录详情
     */
    @GetMapping("/{evaluationId}")
    public Result getEvaluationById(@PathVariable("evaluationId") Integer evaluationId) {
        SupplierEvaluation evaluation = evaluationService.getEvaluationById(evaluationId);
        return Result.success(evaluation);
    }

    /**
     * 新增评价记录
     */
    @PostMapping
    public Result addEvaluation(@Valid @RequestBody SupplierEvaluation evaluation, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError fieldError : errors) {
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return Result.error().add("errorFields", map);
        }

        evaluationService.addEvaluation(evaluation);
        return Result.success();
    }

    /**
     * 修改评价记录
     */
    @PutMapping
    public Result updateEvaluation(@Valid @RequestBody SupplierEvaluation evaluation, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError fieldError : errors) {
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return Result.error().add("errorFields", map);
        }

        evaluationService.updateEvaluation(evaluation);
        return Result.success();
    }

    /**
     * 删除评价记录
     */
    @DeleteMapping("/{evaluationId}")
    public Result deleteEvaluation(@PathVariable("evaluationId") Integer evaluationId) {
        evaluationService.deleteEvaluation(evaluationId);
        return Result.success();
    }
}
